﻿using RSFrame.Services;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;

namespace SqlServerService
{
    class SqlServerService : IDBService
    {
        public SqlServerService(IConfigurationService configurationHelper)
        {
            UpdateConnectionString(configurationHelper.DBAddress);
        }

        public string ConnectionString { get; set; } = string.Empty;

        public object AddData(DbConnection connection, DbCommand command)
        {
            if (connection != null && connection.State == ConnectionState.Open)
            {
                command.Connection = connection;
                return command.ExecuteScalar();
            }
            else
                return null;
        }

        public object AddData(DbConnection connection, string queryText, Dictionary<string, object> paraList)
        {
            DbCommand command = GetCommand(queryText, paraList);
            return AddData(connection, command);
        }

        public void CloseConnection(DbConnection connection)
        {
            if (connection.State == ConnectionState.Open)
                connection.Close();
        }

        public DbConnection EstablishConnection(string connectionString)
        {
            SqlConnection Connection = new(connectionString);
            Connection.Open();
            return Connection;
        }

        public DbConnection EstablishConnection() => EstablishConnection(ConnectionString);

        public DbCommand GetCommand(string queryText, Dictionary<string, object> paraList)
        {
            try
            {
                SqlCommand command = new(queryText);
                if (paraList != null)
                {
                    foreach (var item in paraList)
                        command.Parameters.AddWithValue(item.Key, item.Value);
                }
                return command;
            }
            catch
            {
                return null;
            }
        }

        public DbDataReader ReadData(DbConnection connection, DbCommand command)
        {
            if (connection != null && connection.State == ConnectionState.Open)
            {
                command.Connection = connection;
                return command.ExecuteReader();
            }
            else
                return null;
        }

        public DbDataReader ReadData(DbConnection connection, string queryText, Dictionary<string, object> paraList)
        {
            DbCommand command = GetCommand(queryText, paraList);
            return ReadData(connection, command);
        }

        public void UpdateConnectionString(params string[] parameters)
        {
            switch (parameters.Length)
            {
                case 1:
                    ConnectionString = "Data Source=" + parameters[0] + ";INITIAL CATALOG=RS;" +
                    "Integrated Security=false;Uid=sa;Pwd=heaiQQ200111"; break;
            }

        }

        public int WriteData(DbConnection connection, DbCommand command)
        {
            if (connection != null && connection.State == ConnectionState.Open)
            {
                command.Connection = connection;
                return command.ExecuteNonQuery();
            }
            else
                return -1;
        }

        public int WriteData(DbConnection connection, string queryText, Dictionary<string, object> paraList)
        {
            DbCommand command = GetCommand(queryText, paraList);
            return WriteData(connection, command);
        }
    }
}
